// ========================================================================== // 01 Biblioteki // -------------------------------------------------------------------------- #define _USE_MATH_DEFINES #include #include #include #include #include #pragma comment (lib,"opengl32.lib") #pragma comment (lib,"glu32.lib") #pragma comment( lib, "glut32.lib") // ========================================================================== // 02 Składniki "private" w klasie Form1 // -------------------------------------------------------------------------- HDC hDC; HGLRC hRC; HWND hWnd; // ========================================================================== // 03 Uzupełnienie destruktora ~Form1 // -------------------------------------------------------------------------- if (hRC) { wglMakeCurrent(NULL,NULL); wglDeleteContext(hRC); hRC = NULL; } ReleaseDC(hWnd,hDC); hDC = NULL; // ========================================================================== // 04 Form1_Load // -------------------------------------------------------------------------- GLuint PixelFormat; hWnd = (HWND)(intptr_t)this->panelGL->Handle; hDC = GetDC(hWnd); if (!hDC) { MessageBox::Show( L"Nie moge utwozyc kontekstu urzadzenia GL" ,L"Error",MessageBoxButtons::OK,MessageBoxIcon::Error); return; } BYTE colorBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES); static PIXELFORMATDESCRIPTOR pfd= { //Tells Windows How We Want Things To Be sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL , // Format Must Support OpenGL //PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format colorBits, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 24, // 24Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 }; // Layer Masks Ignored LoadLibrary(L"opengl32.dll"); PixelFormat = ChoosePixelFormat(hDC, &pfd); if (!PixelFormat) { MessageBox::Show( L"Nie moge znalezc odpowiedniego PixelFormat" ,L"Error",MessageBoxButtons::OK,MessageBoxIcon::Error); return; } if (!SetPixelFormat(hDC, PixelFormat, &pfd)) { MessageBox::Show( L"Nie moge ustawic PixelFormat", L"Error",MessageBoxButtons::OK,MessageBoxIcon::Error); return; } hRC = wglCreateContext(hDC); if (!hRC) { MessageBox::Show( L"Nie moge utworzyc kontekstu renderowania./n "+ GetLastError(), L"Error",MessageBoxButtons::OK,MessageBoxIcon::Error); return; } if (!wglMakeCurrent(hDC, hRC)) { MessageBox::Show( L"Nie moge uaktywnic kontekstu renderowania./n "+ GetLastError(), L"Error",MessageBoxButtons::OK,MessageBoxIcon::Error); return; } panelGL_Resize( sender, e); // ========================================================================== // 05 panelGL_Resize // -------------------------------------------------------------------------- ZmianaStanuOpenGL(); GLsizei h = panelGL->Height; GLsizei w = panelGL->Width; if (h == 0) h = 1; // Zabezpiecznie przed dzieleniem przez zero glViewport(0, 0, w, h); // Ustawienie widoku w obszarze roboczym okna glMatrixMode(GL_PROJECTION); glLoadIdentity(); // ......................... // ......................... glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Wyzerowanie układu współrzędnych DrawGLScene(); SwapBuffers(hDC); // ========================================================================== // 06 Definicja głównej funkcji rysującej scenę oraz ustawienia param. rysow. // -------------------------------------------------------------------------- void DrawGLScene( void) { glLoadIdentity(); glClearColor( 0.0, 0.0, 0.0, 1.0); // Ustawienie koloru tła glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Wyczyszczenie okna glPushMatrix(); //................................................................... glColor3f(1.0f, 0.0f, 0.0f); // Kolor rysowania czerwony glRectf(0.0,0.0,0.5,0.5); // Narysowanie prostokąta //................................................................... glPopMatrix(); glFlush(); // Wykonanie kolejki polecen SwapBuffers(hDC); // Podmiana bufora obrazu } void ZmianaStanuOpenGL( void) { glEnable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable( GL_NORMALIZE); } // ========================================================================== // 07 panelGL_Paint // -------------------------------------------------------------------------- DrawGLScene(); // ========================================================================== // 08 hScrollBarSkala_Scroll // -------------------------------------------------------------------------- panelGL_Resize( sender, e); // ========================================================================== // 09 Dodatek do panelGL_Resize // -------------------------------------------------------------------------- glMatrixMode( GL_PROJECTION); glLoadIdentity(); // Ustalenie bryły obcinania (lewa, prawa, dolna, górna, bliższa, dalsza) GLfloat roz=1.0f*pow(10.0,0.05*(50-hScrollBarSkala->Value)); if (w <= h) glOrtho (-roz, roz, -roz*h/w, roz*h/w, -10.f*roz, 10.f*roz); else glOrtho (-roz*w/h, roz*w/h, -roz, roz, -10.f*roz, 10.f*roz); // ========================================================================== // 10 Pierwsze dwie sceny // -------------------------------------------------------------------------- if(radioButton1->Checked==true) glRectf(0.0,0.0,0.5,0.5); else if(radioButton2->Checked==true) { glBegin( GL_TRIANGLES); glVertex3d( 0.0f, 0.5f, 0.0f); glVertex3d(-0.5f, 0.0f, 0.0f); glVertex3d( 0.5f, 0.0f, 0.0f); glEnd(); } // ========================================================================== // 11 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton3->Checked==true) { glBegin( GL_TRIANGLES); glColor3d(0.0f,0.0f,1.0f); glVertex3d( 0.0f, 0.5f, 0.0f); glColor3d(0.0f,1.0f,0.0f); glVertex3d(-0.5f, 0.0f, 0.0f); glColor3d(1.0f,0.0f,0.0f); glVertex3d( 0.5f, 0.0f, 0.0f); glEnd(); } // ========================================================================== // 12 Dodatek do ZmianaStanuOpenGL // -------------------------------------------------------------------------- if(checkBox1->Checked==true) glShadeModel(GL_FLAT); else glShadeModel(GL_SMOOTH); // ========================================================================== // 13 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton4->Checked==true) { glColor3f(1.0f, 0.0f, 0.0f); glBegin( GL_TRIANGLES); glVertex3d( 0.0f, 0.5f, 0.0f); glVertex3d(-0.5f, 0.0f, 0.0f); glVertex3d( 0.5f, 0.0f, 0.0f); glEnd(); glColor3f(1.0f, 1.0f, 0.0f); glBegin( GL_TRIANGLES); glVertex3d( 0.0f, 0.5f, 0.1f); glVertex3d(-0.5f, 0.0f, 0.1f); glVertex3d( 0.5f, 0.0f, 0.1f); glEnd(); } // ========================================================================== // 14 Dodatek do DrawGLScene - obroty // -------------------------------------------------------------------------- GLfloat alfa= hScrollBarAlfa->Value; GLfloat beta= hScrollBarBeta->Value; GLfloat gamma= hScrollBarGamma->Value; glRotatef(alfa, 1.0f, 0.0f, 0.0f); glRotatef(beta, 0.0f, 1.0f, 0.0f); glRotatef(gamma, 0.0f, 0.0f, 1.0f); // ========================================================================== // 15 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton5->Checked==true) { GLfloat x,y,z,angle; for(int j=0; j<2; j++) { //glFrontFace(j==0?GL_CW:GL_CCW); glBegin( GL_TRIANGLE_FAN); // Wachlarze trojkatow glColor3f( 1.0f, 0.0f, 0.0f); glVertex3f( 0.f, 0.f, 1.0f*j); for( int i=0; i<=8; i++) { x=0.5*cos( 2*M_PI*i/8); y=0.5*sin( 2*M_PI*i/8); if((i+j)%2==0) glColor3f( 0.0f, 1.0f, 0.0f); else glColor3f( 1.0f, 0.0f, 0.0f); glVertex3f(x,y,0.0f); } glEnd(); } } // ========================================================================== // 16 Dodatek do ZmianaStanuOpenGL // -------------------------------------------------------------------------- if(checkBox2->Checked==true) glPolygonMode(GL_BACK,GL_LINE); else glPolygonMode(GL_BACK,GL_FILL); if(checkBox3->Checked==true) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); if(checkBox4->Checked==true) { GLfloat otoczenia[] = { 0.3f, 0.3f, 0.3f, 1.0f }; GLfloat rozproszone[]={ 0.7f, 0.7f, 0.7f, 1.0f }; GLfloat odbite[] ={ 1.0f, 1.0f, 1.0f, 1.0f}; GLfloat pozycja[] ={ 150.0f, 150.0f, -50.0f, 1.0f }; glEnable(GL_LIGHTING); // wł. analizę oświetlenia glLightfv( GL_LIGHT0, GL_AMBIENT, otoczenia); // światło oblewające glLightfv( GL_LIGHT0, GL_DIFFUSE, rozproszone);// światło rozproszone glLightfv( GL_LIGHT0, GL_SPECULAR, odbite); // światło odbite glLightfv( GL_LIGHT0, GL_POSITION, pozycja); // pozycja światła glEnable( GL_LIGHT0); // włączenie żarówki nr 0 glEnable(GL_COLOR_MATERIAL); // śledź kolor materiału glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);// właściwości materiału glMaterialfv(GL_FRONT, GL_SPECULAR,odbite); // wszystkie materiały maja glMateriali(GL_FRONT,GL_SHININESS,128); // odblysk z wysokim połyskiem } else glDisable( GL_LIGHTING); // ========================================================================== // 17 Definicja funkcji // -------------------------------------------------------------------------- void Graniastoslup( int n, int kod) { GLfloat x0,z0,x1,z1; for(int i=0; i<=n; i++) { glBegin(GL_QUADS); x0=0.5*cos(i*2.0*M_PI/n); z0=0.5*sin(i*2.0*M_PI/n); x1=0.5*cos((i+1)*2.0*M_PI/n); z1=0.5*sin((i+1)*2.0*M_PI/n); if(kod==1) glNormal3f(x0+x1, 0.0f, z0+z1); else if(kod==2) glNormal3f(x0, 0.0f, z0); glVertex3f(x0,-0.5f, z0); glVertex3f(x0, 0.5f, z0); if(kod==2) glNormal3f(x1, 0.0f, z1); glVertex3f(x1, 0.5f, z1); glVertex3f(x1,-0.5f, z1); glEnd(); } } // ========================================================================== // 18 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton6->Checked==true) Graniastoslup( 6,0); // ========================================================================== // 19 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton7->Checked==true) Graniastoslup( 6,1); // ========================================================================== // 20 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton8->Checked==true) Graniastoslup( 20,1); // ========================================================================== // 21 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton9->Checked==true) Graniastoslup( 20,2); // ========================================================================== // 22 Definicja funkcji Cylinder oraz Czasteczka // -------------------------------------------------------------------------- void Cylinder(GLfloat promien, GLfloat wysokosc, GLint n) { glPushMatrix(); glScalef( 2.0f*promien, wysokosc, 2.0f*promien); Graniastoslup(n, 2); glPopMatrix(); } void Czasteczka( void) { glPushMatrix(); glColor3d( 0.0f, 1.0f, 0.0f); glTranslatef(-0.6f,0.0f,0.0f); glutSolidSphere(0.35f,40,20); glPopMatrix(); glPushMatrix(); glColor3d( 0.0f, 0.6f, 0.6f); glTranslatef( 0.6f,0.0f,0.0f); glutSolidSphere(0.35f,40,20); glPopMatrix(); glPushMatrix(); glColor3d( 1.0f, 0.0f, 0.0f); glRotatef(90.0f, 0.0f, 0.0f, 1.0f); Cylinder(0.1f, 0.8f, 20); glPopMatrix(); } // ========================================================================== // 23 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton10->Checked==true) Czasteczka(); // ========================================================================== // 24 Przeróbka w panelGL_Resize // -------------------------------------------------------------------------- if(checkBox5->Checked==false) { //A } else { GLfloat katWidz=50.0f, fAspect = 1.0f*w/h; gluPerspective(katWidz, fAspect, 0.1, 2000.0); // Rzutowania perspektyw. glTranslatef(0.0f,0.0f,-2.2f); //B } // ========================================================================== // 25 Definicja funkcji Podloga, Pudelko oraz Filar // -------------------------------------------------------------------------- void Podloga( GLfloat ax, GLfloat az, int nx, int nz) { GLfloat x,z; glBegin(GL_LINES); z=0.5f*az; for(int i=0; i<=nx; i++) { x=-0.5f*ax+i*ax/nx; glVertex3f(x, 0.0f, -z); glVertex3f(x, 0.0f, +z); } x=0.5f*ax; for(int i=0; i<=nx; i++) { z=-0.5f*az+i*az/nz; glVertex3f(+x, 0.0f, z); glVertex3f(-x, 0.0f, z); } glEnd(); } void Pudelko(GLfloat szer,GLfloat wys,GLfloat dlug) { glPushMatrix(); glScalef(szer,wys,dlug); glutSolidCube( 1.0f); glPopMatrix(); } void Filar( GLfloat hp, GLfloat hr, GLfloat a, GLfloat r , int n) { glPushMatrix(); glTranslatef( 0.0f, 0.5f*hp, 0.0f); Pudelko(a,hp,a); glTranslatef( 0.0f,hp+hr,0.0f); Pudelko(a,hp,a); glTranslatef( 0.0f,-0.5f*(hp+hr),0.0f); glScalef(r,hr,r); Graniastoslup( n,1); glPopMatrix(); } // ========================================================================== // 26 Kolejna scena // -------------------------------------------------------------------------- else if(radioButton11->Checked==true) { int Px[10]={-14,-3,6,12,-17,8,8,-9,-19,-8}; int Pz[10]={-4,9,-3,6,12,-17,8,8,-9,-19}; glColor3f(0.0f, 0.0f, 1.0f); Podloga( 40.0f, 40.0f, 40, 40); Czasteczka(); glColor3f(1.0f, 0.0f, 0.0f); for(int i=0; i<10; i++) { glPushMatrix(); glTranslatef(Px[i],0,Pz[i]); Filar(0.2,1.5, 0.8, 0.35 , 8); glPopMatrix(); } glPushMatrix(); glTranslatef(-3,0,3); glColor3f(0.2f, 0.2f, 0.8f); glutSolidTorus(0.3f,0.8f,20,40); glTranslatef(6,0,0); glColor3f(0.8f, 0.8f, 0.1f); glFrontFace(GL_CW); glutSolidTeapot(1.5f); glFrontFace(GL_CCW); glPopMatrix(); glPushMatrix(); glTranslatef(0.0f,0.5f,-5.0f); glColor3f(0.8f, 0.0f, 1.0f); Pudelko(3.0f,1.0f,3.0f); glTranslatef(0.0f,1.5f+WysKulki,0.0f); glColor3f(0.5f, 0.3f, 1.0f); glutSolidSphere(1.0f,40,20); glPopMatrix(); // C } // ========================================================================== // 27 Znienne globalne // -------------------------------------------------------------------------- GLfloat kier=-0.5*M_PI, eyeX=0.0f, eyeZ=0.0f, eyeY=0.0f; //ZMIENNE GLOBALNE // ========================================================================== // 28 Dodatek do panelGL_Resize // -------------------------------------------------------------------------- gluLookAt( eyeX, eyeY,eyeZ, eyeX+GLfloat(cos(kier)), eyeY, eyeZ+GLfloat(sin(kier)), 0.0f, 1.0f, 0.0f); // Ulozenie kamery: 1-3 polozenie kamery, 4-6 - punkt patrzenia // 7-9 gora kamery // ========================================================================== // 29 Form1_KeyDown // -------------------------------------------------------------------------- if(e->KeyCode==Keys::Home) eyeY+=0.2f; else if(e->KeyCode==Keys::End) eyeY-=0.2f; else if(e->KeyCode==Keys::Left) kier-=GLfloat(M_PI/30); else if(e->KeyCode==Keys::Right) kier+=GLfloat(M_PI/30); else if(e->KeyCode==Keys::Up) {eyeX+=GLfloat(cos(kier)); eyeZ+=GLfloat(sin(kier));} else if(e->KeyCode==Keys::Down) {eyeX-=GLfloat(cos(kier)); eyeZ-=GLfloat(sin(kier));} else if(e->KeyCode==Keys::Escape)Close(); else if(e->KeyCode==Keys::S) checkBox4->Checked=!checkBox4->Checked; else if(e->KeyCode==Keys::C) checkBox1->Checked=!checkBox1->Checked; // if(eyeX>20.0f) eyeX=20.0f; if(eyeX<-20.0f) eyeX=-20.0f; if(eyeZ>20.0f) eyeZ=20.0f; if(eyeZ<-20.0f) eyeZ=-20.0f; panelGL_Resize(sender,e) ; // ========================================================================== // 30 Znienne globalne // -------------------------------------------------------------------------- GLfloat WysKulki=0.0; // ========================================================================== // 31 Timer1_Tick // -------------------------------------------------------------------------- static GLfloat T=-1.0; T+=0.02; if(T>=1.0) T-=2.0f; WysKulki=5.0f*(1.0f-T*T); panelGL_Resize(sender,e) ; // ========================================================================== // 32 Uzupełnić ostatnią scenę // -------------------------------------------------------------------------- glPushMatrix(); glTranslatef(0.0f,0.5f,-5.0f); glColor3f(0.8f, 0.0f, 1.0f); Pudelko(3.0f,1.0f,3.0f); glTranslatef(0.0f,1.5f+WysKulki,0.0f); glColor3f(0.5f, 0.3f, 1.0f); glutSolidSphere(1.0f,40,20); glPopMatrix(); // ========================================================================== // KONIEC. Autor: Sylwester Ptzybył // ==========================================================================